<template>
  <div class="assessment-edit-plan-container">
    <div class="my-form-container">

      <el-row class="my-form-item" style="height: 20%">
        <el-col :span="12">
          <div class="my-form-item-label">姓 名</div>
          <div class="my-form-item-value">{{ tablelist.name }}</div>
        </el-col>
        <el-col :span="12">
          <div class="my-form-item-label">学 号：</div>
          <div class="my-form-item-value">{{ tablelist.studentno }}</div>
        </el-col>
      </el-row>
      <el-row class="my-form-item" style="height: 20%">
        <el-col :span="12">
          <div class="my-form-item-label">级 次：</div>
          <div class="my-form-item-value"> {{ tablelist.level }}</div>
        </el-col>
        <el-col :span="12">
          <div class="my-form-item-label">队 别</div>
          <div class="my-form-item-value"> {{ tablelist.team }}</div>
        </el-col>
      </el-row>
      <el-row class="my-form-item" style="height: 20%">
        <el-col :span="12">
          <div class="my-form-item-label">专 业：</div>
          <div class="my-form-item-value"> {{ tablelist.major }}</div>
        </el-col>
        <el-col :span="12">
          <div class="my-form-item-label">选择缓考课程</div>
          <div class="cz-save " @click=" getSelectJob()">
            <span>请选择</span>
          </div>
        </el-col>
      </el-row>
      <el-row class="my-form-item" style="height: 10%">
        <el-col :span="24">
          <div class="my-form-item-label">上传材料证明</div>
          <div class="my-form-item-value">
            <div v-if="tablelist.filename" class="fileClass">
              <span>{{ tablelist.filename }}</span>
              <input type="file" @change="getFile($event)" class="save"/>

            </div>
            <input v-else type="file" @change="getFile($event)" class="cz-save"
                   style="margin-top: 0px;margin-left: 0px;"/>

          </div>
        </el-col>

      </el-row>


      <el-row class="my-form-item" style="height: 30%;border: none">
        <el-col :span="24">
          <div class="my-form-item-label">缓考原因：</div>
          <div class="my-form-item-value assessment-design">
            <el-input v-model="tablelist.hkdetails" type="textarea" :rows="9" style="width: 969px;"
                      placeholder="请输入内容"></el-input>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="button-group">
      <div class="" @click="getsubmit('保存')">保存</div>
      <div class="" @click="getsubmit('提交')">保存并提交</div>
    </div>
    <el-dialog title="选择缓考课程" :visible.sync="showYprDialog" width="983px" style="height:832px" class="szjky">
      <div class="choose-ypr-container">
        <div class="choose-ypr-search-container">
          <span class="title">2021年秋季学期课程：</span>

          <div class="button-list">
            <div
              class="button-item"
              v-for="(item, index) in courselists"
              :key="index"
              @click="delTag(index)"
              style="width: 104px"
            >
              <img
                class="del-icon"
                src="../../../assets/assessment/approval-del.png"
                alt=""
              />
              <div>{{ item.coursename }}</div>
            </div>
          </div>
        </div>
        <div class="choose-ypr-table-container">
          <el-table
            ref="multipleTable"
            :data="yprList"
            border
            style="width: 100% "
            class="table-data"
            row-class-name="row-class"
            cell-class-name="cell-class"
            header-row-class-name="header-row-class"
            header-cell-class-name="header-cell-class"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              prop=""
              label="选择"
              width="122"
              align="center"
              class-name="" type="selection"

              label-class-name=""
            >
            </el-table-column>
            <el-table-column
              prop="department"
              label="系"
              width="122"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="teachingandresearchsection" label="研究室" width="122" align="center">
            </el-table-column>
            <el-table-column
              prop="coursenumber"
              label="课程编号"
              width="122"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="coursename" label="课程名称" width="122" align="center">
            </el-table-column>
            <el-table-column prop="teachingfaculty" label="课程教员" width="122" align="center">
            </el-table-column>
            <el-table-column prop="exadate" label="考试日期" width="122" align="center">
            </el-table-column>
            <el-table-column prop="exaschedule" label="节次" align="center">
            </el-table-column>
          </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <button class="green" @click="showYprDialog = false">确 定</button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import {createDId, detail, getsave, getUpload, hkkcList, submit,getCurrentOne} from "@/api/examination/approvalDelayedExam";



export default {
  data() {
    return {
      showYprDialog: false,
      searchNameList: [],
      courselists: [],
      yprList: [],
      fileName: '',
      design: '',
      xq: "请选择",
      tablelist: {
        name: "1",
        studentno: "2",
        level: "2",
        team: "2",
        major: "2",
        courselists: [],
        hkdetails: ''

      },
      courselistsList:'',
      xqList: [
        {
          value: "英语",
          label: "英语",
        },
        {
          value: "数学",
          label: "数学",
        },
      ],
    }

  },
  mounted() {
    // this.getdetail()
    // this.getCreateDId(this.tablelist)
    this.getCurrentOne()
  },
  methods: {
    getCurrentOne() {
      return new Promise(() => {
        getCurrentOne().then(res => {
          let data = res.data.data
          this.tablelist = {
            name: data.studentName,
            studentno: data.studentCode,
            level: data.specialtyClassInfo.year,
            team: data.specialtyClassInfo.studentTeam,
            major: data.specialtyClassInfo.className,
            courselists: [],
            hkdetails: ''
          }
          this.getCreateDId(this.tablelist)
          // console.log(res.data.data)

        })
      })
    },
    getCreateDId(obj) {
      return new Promise(() => {
        createDId(obj).then(res => {
          this.tablelist.id = res.data.data
          // console.log(res.data.data)

        })
      })
    },
    getsubmit(type) {
      console.log(type)
      let obj = this.tablelist

      if (obj.courselistsDto.length == 0) {
        this.$message({
          showClose: true,
          message: "选择缓考课程不能为空",
          type: "error",
        });
        return
      }
      if (!this.tablelist.fileName) {
        this.$message({
          showClose: true,
          message: "上传材料证明未上传",
          type: "error",
        });
        return
      }
      if (!this.tablelist.hkdetails) {
        this.$message({
          showClose: true,
          message: "缓考原因未填写",
          type: "error",
        });
        return
      }
      this.tablelist.xueqi='2021春'
      let message
      let el = this
      if (type === '保存') {
        this.tablelist.status = -1
        message = '保存成功'
      } else if (type === '提交') {
        message = '保存并提交成功'
        this.tablelist.status = 0
      }
      return new Promise(() => {
        getsave(obj).then(res => {
          if (res.data.code === 200) {
            el.$message({
              showClose: true,
              message: message,
              type: "success",
            });
            this.$router.push("/student/approvalDelayedExam/approvalDelayedExam");
          }
        })
      })
    },
    getFile(event) {
      let file = event.target.files;
      console.log(this.tablelist.fileName)
      // this.fileName=file
      let form = new FormData();
      let id = this.tablelist.id
      form.append("file", file[0]);
      form.append("id", id);
      let el = this
      return new Promise(() => {
        getUpload(form).then(res => {
          if (res.data.code === 200) {
            this.tablelist.fileName = file[0].name
            // this.getdetail()
            el.$message({
              showClose: true,
              message: "上传成功",
              type: "success",
            });
          } else {
            el.$message({
              showClose: true,
              message: "上传失败，请重新上传",
              type: "error",
            });
          }
        })
      })
    },
    delTag(index) {
      let el = this

      // this.$refs.multipleTable.clearSelection(this.searchNameList[index]);
      el.$nextTick(() => {
        el.$refs.multipleTable.toggleRowSelection(
          el.yprList.find(item => {
              return this.courselists[index] == item.id;
            }
          ), false);

      })
    },

    getSelectJob() {
      return new Promise(() => {
        let el = this
        hkkcList().then(res => {
          if (res.data.code === 200) {
            this.yprList = res.data.data
            this.showYprDialog = true
            console.log(res.data.data)
            el.$nextTick(() => {
              el.courselistsList.forEach(row => {
                el.$refs.multipleTable.toggleRowSelection(
                  this.yprList.find(item => {
                      return row == item.id;
                    }
                  ), true);
              });
            })
          }
        })
      })

    },
    getdetail() {
      let obj = {
        id: this.tablelist.id
      }
      return new Promise(() => {
        detail(obj).then(res => {
          if (res.data.code === 200) {
            let arr = []
            this.tablelist = res.data.data
            this.tablelist.filename = this.tablelist.filepath.split(';')[1];

            let courselists = this.tablelist.courselists
            for (let i = 0; i < courselists.length; i++) {
              arr.push(courselists[i].matchid)
            }
            this.courselistsList = arr
            console.log(arr)
          }
        })
      })
    },
    handleSelectionChange(val) {
      let arr=[]
      this.courselists = val
      this.tablelist.courselistsDto = val
      for (let i = 0; i < val.length; i++) {
        this.tablelist.courselistsDto[i].matchid=val[i].id
        arr.push(val[i].id)
      }
      if(arr.length){
        this.courselistsList=arr
      }

      console.log(this.tablelist, 'this.tablelist')
    }
  }
};
</script>
<style lang="less" scoped>
.cz-save {
  box-sizing: border-box;


  line-height: 44px;
  border-radius: 4px;
  font-size: 12px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  text-align: center;
  margin-top: 70px;
  margin-left: 40px;
  display: flex;
  flex-direction: row;
  cursor: pointer;
  align-content: center;
  justify-content: center;
  align-items: center;

}

.fileClass {
  position: relative;
  cursor: pointer;

  .save {
    width: 120px;
    height: 50px;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    color: #fff;
    background: #303133;
  }
}


.my-form-item-value {
  font-size: 16px;
  color: #004CA7;
  height: 100%;
  flex: 1;
  line-height: 60px;
  padding-left: 38px;
  display: flex;
  align-items: center;
}

.assessment-edit-plan-container {
  .assessment-design {
    .el-textarea__inner {
      resize: none;
      font-size: 16px;
      color: rgba(0, 76, 167, 1);
    }
  }
}
</style>
<style scoped lang="less">
.assessment-edit-plan-container {
  .assessment-design {
    padding: 25px 38px !important;


  }

  .button-group {
    text-align: center;

    > div {
      display: inline-block;
      font-size: 18px;
      padding: 6px 22px;
      border-radius: 6px;

      &:nth-child(1) {
        color: #F3B815;
        border: 1px solid #F3B815;
        background: #FFF6EF;
        margin-right: 139px;
      }

      &:nth-child(2) {
        color: #02C86C;
        border: 1px solid #02C86C;
        background: #F1FFF8;
      }
    }
  }
}
</style>
